function cancelSale() {
	location.href = "home";
}

function clickSelectFile() {
	$('#pic').click();

}

function selectFile(){
    var files = document.getElementById('pic').files;
    console.log(files[0]);
    if(files.length == 0){
        return;
    }
    var file = files[0];
    var reader = new FileReader();
    console.log(reader);
    reader.readAsBinaryString(file);
    reader.onload = function(f){
        var result = document.getElementById("sale_image_container");
        var src = "data:" + file.type + ";base64," + window.btoa(this.result);
        result.innerHTML = '<img id="sale_image" src ="'+src+'"/>';

    }
    console.log('file',file);
    form.append('file',file);
    console.log(form.get('file'));
}

function addSale() {
	location.href = "sale_add";
}
function onSubmitUpdate(op,saleID){
	document.getElementById('sale_id').value=saleID;
	var form = document.getElementById('saleForm');
	form.action="sale/"+op;
	form.submit();
}
function showPreview(source) {
    var file = source.files[0];
    if(window.FileReader) {
        var fr = new FileReader();
        var portrait = document.getElementById('portrait');
        fr.onloadend = function(e) {
          portrait.src = e.target.result;
        };
        fr.readAsDataURL(file);
        portrait.style.display = 'block';
    }
  }
function onSubmitGo(){

	window.history.go(-1);

}
function onSubmitDel(op,saleID){
	document.getElementById('sale_id').value=saleID;
	if (false === confirm('削除しますか？') )return;
    var form = document.getElementById('saleForm');
    form.action="/admin/sale/"+op;
    form.submit();
}
function saveSale(op) {

	var sale_name = document.getElementById("sale_add_names_input");
	var start_date = document.getElementById("start_date");
	var end_date = document.getElementById("end_date");
	var pic = document.getElementById("pic");
	var goodsId = document.getElementById("goodsNameId");
	var num1 = document.getElementById("num1");
	var data1 = document.getElementById("data1");
	var num2 = document.getElementById("num2");
	var data2 = document.getElementById("data2");
	var num3 = document.getElementById("num3");
	var data3 = document.getElementById("data3");
	var corpId = document.getElementById("corpId");

	if(trim(sale_name.value)==null || trim(sale_name.value)==""){
		alert("セール名称を入力してください!");
		sale_name.focus();
		return false;
	}
	if(trim(start_date.value)==null || trim(start_date.value)==""){
		alert("開始日期を入力してください!");
		start_date.focus();
		return false;
	}
	if(trim(end_date.value)==null || trim(end_date.value)==""){
		alert("終了日期を入力してください!");
		end_date.focus();
		return false;
	}
	if(trim(pic.value)==null || trim(pic.value)==""){
		alert("画像を選択してください!");
		pic.focus();
		return false;
	}
	if(trim(goodsId.value)==null || trim(goodsId.value)==""){
		alert("商品コードを選択してください!");
		goodsId.focus();
		return false;
	}
	if(trim(num1.value)==null || trim(num1.value)==""){
		alert("条件種類1の数量を入力してください!");
		num1.focus();
		return false;
	}
	if(trim(data1.value)==null || trim(data1.value)==""){
		alert("条件種類1の割引额を入力してください!");
		data1.focus();
		return false;
	}
	if(trim(num2.value)==null || trim(num2.value)==""){
		alert("条件種類2の数量を入力してください!");
		num2.focus();
		return false;
	}
	if(trim(data2.value)==null || trim(data2.value)==""){
		alert("条件種類2の割引额を入力してください!");
		data2.focus();
		return false;
	}
	if(trim(num3.value)==null || trim(num3.value)==""){
		alert("条件種類3の数量を入力してください!");
		num3.focus();
		return false;
	}
	if(trim(data3.value)==null || trim(data3.value)==""){
		alert("条件種類3の割引额を入力してください!");
		data3.focus();
		return false;
	}
	if(trim(corpId.value)==null || trim(corpId.value)==""){
		alert("適用対象を選択してください!");
		corpId.focus();
		return false;
	}
	var form = document.getElementById('saleForm');
	form.action="sale/"+op;
	form.submit();
}
function trim(str){

	   return str.replace(/(^\s*)|(\s*$)/g, "");

}
function saveUpdateSale() {
	//document.getElementById('sale_id').value=saleID;
	var sale_name = document.getElementById("sale_add_names_input");
	var start_date = document.getElementById("start_date");
	var end_date = document.getElementById("end_date");

	if(trim(sale_name.value)==null || trim(sale_name.value)==""){
		alert("セール名称を入力してください!");
		sale_name.focus();
		return false;
	}
	if(trim(start_date.value)==null || trim(start_date.value)==""){
		alert("開始日期を入力してください!");
		start_date.focus();
		return false;
	}
	if(trim(end_date.value)==null || trim(end_date.value)==""){
		alert("終了日期を入力してください!");
		end_date.focus();
		return false;
	}

	var form = document.getElementById('saleForm');
	form.action="../sale/addupdate";
	form.submit();
}
function cancelUpdateSale () {
	location.href = "../home";
}
function cancelAddSale () {
	location.href = "home";
}

function onSubmitAddSet() {
	location.href = "set_add";
}
